package com.ssms.controller;

import com.ssms.commons.Result;
import com.ssms.commons.TablePage;
import com.ssms.model.Exam;
import com.ssms.model.SysUser;
import com.ssms.service.ClazzService;
import com.ssms.service.ExamService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Resource
    private ExamService examService;

    @RequestMapping()
    public String exam() {
        return "exam";
    }

    @RequestMapping("list")
    @ResponseBody
    public TablePage list(TablePage tablePage,
//                          @RequestParam(value = "clazzId") Integer clazzId,
                          Exam exam) {
        return examService.search(tablePage, exam);
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(Exam exam, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        Integer instor = null;
        // 只有是辅导员时，才传入其id
        if(user.getRoleType() == 2){
            instor = user.getId();
        }
        return examService.save(exam,instor);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(Exam exam, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        Integer instor = null;
        if(user.getRoleType() == 2){
            instor = user.getId();
        }
        return examService.update(exam,instor);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Integer id) {
        return examService.delete(id);
    }

}
